<?php
/**
 * Model Note
 */

class Model_Note extends Model_Abstract {

    /**
     * 添加一条新留言
     * @param type $username
     * @param type $message
     * @return bool
     */
    public static function postNew($username, $message) {
        
        $data = array(
            'username' => $username,
            'message' => $message,
        );
        try {
            $daoNote = new DAO_Note();
            return $daoNote->add($data);
        } catch (Exception $ex) {
            $e = new Model_Exception('add note failed: '.$ex->getMessage(), Model_Exception::E_MODEL_DAO_OPERATION_ERROR);
            V::log($e);
            throw $e;
        }
    }
    
    /**
     * 按分页获取留言记录列表
     * @param type $page
     * @param type $pageSize
     * @return array
     */
    public static function getNoteList($page, $pageSize) {
        
        $page = intval($page);
        $pageSize = intval($pageSize);
        if ($page < 1 || $pageSize < 1) {
            throw new Model_Exception('bad page/pageSize param.', Model_Exception::E_PARAM_ERROR);
        }

        try {
            $offset = ($page - 1) * $pageSize;
            $daoNote = new DAO_Note();
            $count = $daoNote->countAll();
            if ($offset >= $count) {
                return ['count' => $count, 'list' => array()];
            }
            $list = $daoNote->listByCreateTimeDesc($offset, $pageSize);
            return ['count' => $count, 'list' => $list];
        } catch (Exception $ex) {
            $e = new Model_Exception('get note list failed: '.$ex->getMessage(), Model_Exception::E_MODEL_DAO_OPERATION_ERROR);
            V::log($e);
            throw $e;
        }
    }
    
}